﻿    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Model;
    using DBUtility;
    using MySql.Data.MySqlClient;

    namespace DAL
    {
        public class ProcessFlowRepository
        {
            // 添加工艺流程
            public int AddProcessFlow(ProcessFlow processFlow)
            {
                string sql = @"INSERT INTO processflow 
                                (StationId, FlowName, StepNumber, ProcessFlowVersion, Description) 
                               VALUES 
                                (@StationId, @FlowName, @StepNumber, @ProcessFlowVersion, @Description)";

                MySqlParameter[] parameters = {
                    new MySqlParameter("@StationId", processFlow.StationId),
                    new MySqlParameter("@FlowName", processFlow.FlowName),
                    new MySqlParameter("@StepNumber", processFlow.StepNumber),
                    new MySqlParameter("@ProcessFlowVersion", processFlow.ProcessFlowVersion),
                    new MySqlParameter("@Description", processFlow.Description)
                };

                return DBHelper.ExecuteNonQuery(sql, parameters);
            }

            // 删除工艺流程
            public int DeleteProcessFlow(int flowId)
            {
                string sql = "DELETE FROM processflow WHERE FlowId = @FlowId";
                MySqlParameter[] parameters = { new MySqlParameter("@FlowId", flowId) };
                return DBHelper.ExecuteNonQuery(sql, parameters);
            }

            // 更新工艺流程信息
            public int UpdateProcessFlow(ProcessFlow processFlow)
            {
                string sql = @"UPDATE processflow 
                                SET StationId = @StationId, 
                                    FlowName = @FlowName, 
                                    StepNumber = @StepNumber, 
                                    ProcessFlowVersion = @ProcessFlowVersion, 
                                    Description = @Description 
                              WHERE FlowId = @FlowId";

                MySqlParameter[] parameters = {
                    new MySqlParameter("@StationId", processFlow.StationId),
                    new MySqlParameter("@FlowName", processFlow.FlowName),
                    new MySqlParameter("@StepNumber", processFlow.StepNumber),
                    new MySqlParameter("@ProcessFlowVersion", processFlow.ProcessFlowVersion),
                    new MySqlParameter("@Description", processFlow.Description),
                    new MySqlParameter("@FlowId", processFlow.FlowId)
                };

                return DBHelper.ExecuteNonQuery(sql, parameters);
            }

            // 查询所有工艺流程
            public List<ProcessFlow> GetAllProcessFlows()
            {
                string sql = "SELECT * FROM processflow";
                return DBHelper.ExecuteQuery(sql, null, MapProcessFlow);
            }

            // 查询单个工艺流程
            public ProcessFlow GetProcessFlowById(int flowId)
            {
                string sql = "SELECT * FROM processflow WHERE FlowId = @FlowId";
                MySqlParameter[] parameters = { new MySqlParameter("@FlowId", flowId) };
                var processFlows = DBHelper.ExecuteQuery(sql, parameters, MapProcessFlow);
                return processFlows.FirstOrDefault();
            }
            // 根据工站 ID 获取工艺流程 ID
            public int GetFlowIdByStationId(int stationId)
            {
                string sql = @"SELECT FlowId FROM processflow WHERE StationId = @StationId";

                MySqlParameter[] parameters = { new MySqlParameter("@StationId", stationId) };

                var result = DBHelper.ExecuteScalar(sql, parameters);
                return result != null ? Convert.ToInt32(result) : -1; // 返回-1表示未找到
            }
            // 映射数据库查询结果到 ProcessFlow 对象
            private ProcessFlow MapProcessFlow(MySqlDataReader reader)
            {
                return new ProcessFlow
                {
                    FlowId = (int)reader["FlowId"],
                    StationId = (int)reader["StationId"],
                    FlowName = reader["FlowName"].ToString(),
                    StepNumber = reader["StepNumber"].ToString(),
                    ProcessFlowVersion = reader["ProcessFlowVersion"].ToString(),
                    Description = reader["Description"] is DBNull ? null : reader["Description"].ToString()
                };
            }
        }
    }